#include "service.h"

CService::CService():m_ssl(0){
	WSAStartup(MAKEWORD(1,1), &WSAData);
}

CService::~CService(){
	
}

bool CService::initialize(CDataStream * n_DataStream){
	m_ssl = new CSSLClient;
	m_ssl->initialize(g_Config.m_SCA);
	if(!get_resource())
		return false;
	return true;
}

void CService::run(){
	start();
}

void CService::execute(){
	m_ssl->connect(this->m_Addr, this->m_Port);
	int ret;
	for(;;){
		ret = m_DataStream->read_buf(m_TempBuf, m_Length);
		if(ret){
			m_Request = "FUP*FILENAME";
			m_ssl->send(m_Request, strlen(m_Request));
		}
	}
}

bool CService::get_resource(){
	hostent * phostent = gethostbyname(g_Config.m_DSDNS);
	m_Addr = inet_ntoa(*(struct in_addr*)phostent->h_addr_list[0]);
	m_ssl->connect(m_Addr, g_Config.m_DSPort);
	m_Request = "VERSION";
	m_ssl->send(m_Request, strlen(m_Request));
	m_ssl->receive(m_Request, 1024);
	if(!strcmp(m_Request, "VERSION*OK*")){
		return false;
	}
	m_Request = "LOGIN*USERNAME*PASSWORD*GUID*";
	m_ssl->send(m_Request, strlen(m_Request));
	m_ssl->receive(m_Request, 1024);
	if(!strcmp(m_Request, "LOGIN*OK*")){
		return false;
	}
	m_Request = "REQUEST*UPLOAD*FILENUMBLE*FILESIZE*";
	m_ssl->send(m_Request, strlen(m_Request));
	m_ssl->receive(m_Request, 1024);
	if(!strcmp(m_Request, "LOGIN*OK*")){
		return false;
	}
	return true;
}